# from fastapi import FastAPI, Body
# from pydantic import BaseModel
# from typing import List, Dict

# app = FastAPI()

# class User(BaseModel):
#     name: str
#     age: int
#     sex: str
#     grade: str

# @app.post("/4.1.1 无嵌入式单个请求体/")
# async def create_user(user: User):
#     message = f'用户{user.name}，年龄{user.age} 创建成功！'
#     return message

# @app.post("/4.1.2 嵌入式单个请求体/")
# async def generate_user(user: User=Body(embed=True)):
#     message = f'用户{user.name}，年龄{user.age} 创建成功！'
#     return message

# @app.post("/4.1.3 列表请求体")
# async def more_user(user:List[User]):
#     message = len(user)
#     return message

# @app.post("/4.1.4 dict 构成的请求体")
# async def dict_user(user:Dict[str, int]):
#     message = 'dict'
#     return message

from typing import Union
from fastapi import Body, FastAPI
from pydantic import BaseModel, Field

app = FastAPI()

class Item(BaseModel):
    name: str = Field(examples=["Foo"])
    description: Union[str, None] = Field(default=None, examples=["A very nice Item"])
    price: float = Field(examples=[35.4])
    tax: Union[float, None] = Field(default=None, examples=[3.2])

@app.put("/items/{item_id}")
async def update_item(item_id: int, item: Item):
    results = {"item_id": item_id, "item": item}
    return results